package com.simulation.superlotto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.simulation.superlotto.common.exception.Asserts;
import com.simulation.superlotto.common.utils.SuperlottoUtil;
import com.simulation.superlotto.common.utils.TimeUtils;
import com.simulation.superlotto.dao.ContrastDao;
import com.simulation.superlotto.dao.SuperlottoDao;
import com.simulation.superlotto.dao.WinnDao;
import com.simulation.superlotto.model.*;
import com.simulation.superlotto.service.SuperlottoService;
import com.simulation.superlotto.vo.ResSuperlottoVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：黄坤
 * @date ：创建于 2020/12/1
 * @description ：
 * @version: 1.0
 */
@Service
public class SuperlottoServiceImpl implements SuperlottoService {

    @Autowired
    private SuperlottoDao superlottoDao;
    @Autowired
    private ContrastDao contrastDao;
    @Autowired
    private WinnDao winnDao;

    @Override
    public Superlotto superlotto(int number) {
        Superlotto su = new Superlotto();
        //创建时间
        su.setCreationTime(TimeUtils.getStringTime(new Date()));
        //预计花费金额
        su.setExpenditureMoney(number*2);
        //随机注数
        su.setNoteNum(number);
       // su.setUserId();
        //归属期数
        //开奖时间
        su = SuperlottoUtil.contextLoads(su);
        Asserts.fail(superlottoDao.insert(su));
        long id= superlottoDao.selectbylastId();
        List<Contrast> contrasts =contrasts(id,number);
        su.setList(contrasts);
        return su;
    }

    @Override
    public Superlotto superlotto(String userName, int number) {
        //根据用户名获取用户id
        User user= superlottoDao.selectUserId(userName);
        if (StringUtils.isEmpty(user)){
            //提示需要添加用户
        }
        Superlotto su = new Superlotto();
        //创建时间
        su.setCreationTime(TimeUtils.getStringTime(new Date()));
        //预计花费金额
        su.setExpenditureMoney(number*2);
        //随机注数
        su.setNoteNum(number);
        su.setUserId(user.getUserId());
        //归属期数
        //开奖时间
        su = SuperlottoUtil.contextLoads(su);
        Asserts.fail(superlottoDao.insert(su));
        long id= superlottoDao.selectbylastId();
        List<Contrast> contrasts =contrasts(id,number);
        su.setList(contrasts);
        return su;
    }


    public String getRedBall(){
        TreeSet<String> set = new TreeSet<>();
        while (set.size()!=5){
            int proparea = new Random().nextInt(25)+1;
            String p=proparea<10? String.format("%02d", proparea): String.valueOf(proparea);
            set.add(p);
        }
        String join = String.join(",", set);
        return join;
    }
    public String getBlueBall(){
        TreeSet<String> hashSet = new TreeSet<>();
        while (hashSet.size()!=2){
            int proparea = new Random().nextInt(12)+1;
            String p=proparea<10? String.format("%02d", proparea): String.valueOf(proparea);
            hashSet.add(p);
        }
        String join = String.join(",", hashSet);
        return join;
    }






    public List<Contrast> contrasts(long superId,int number){

        List<Contrast> contrasts = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            Contrast contrast = new Contrast();
            contrast.setRedBall(getRedBall());
            contrast.setBlueBall(getBlueBall());
            contrast.setSuperId(superId);
            contrasts.add(contrast);
            Asserts.fail(contrastDao.insert(contrast));
        }
       return contrasts;

    }

    public void judge(Contrast c, int periodsNum){
        List<Contrast> list = contrastDao.selectList(new QueryWrapper<Contrast>().inSql("super_id", "select super_id from superlotto s  where s.periods_num=" + periodsNum));
        List<String> redBall = new ArrayList<>(Arrays.asList(c.getRedBall().split(","))) ;
        List<String> blueBall = new ArrayList<>(Arrays.asList(c.getBlueBall().split(",")));

        HashMap<String, Integer> map = new HashMap<>();
        for (Contrast contrast : list) {
            List<String> red =  new ArrayList<>(Arrays.asList(contrast.getRedBall().split(",")));
            List<String> blue = new ArrayList<>(Arrays.asList(contrast.getBlueBall().split(",")));

            red.removeAll(redBall);
            blue.removeAll(blueBall);
            int a=5-red.size();
            int b=2-blue.size();
            System.out.println("id:"+contrast.getContrastId()+"红球:"+a+" 蓝球: "+b);
                Bonus bonus= contrastDao.selectMoney(a,b);
            String bonusName = bonus.getBonusName();
            Integer m = map.get(bonusName);
            if (m!=null && m!=0){
                map.put(bonusName,m+1);
                }else {
                map.put(bonusName,1);
                }
        }
        System.out.println("共计"+list.size()+"注,"+list.size()*2+"元");
        for (String s : map.keySet()) {
            long l=contrastDao.selectbonusName(s);
            Integer v = map.get(s);
            System.out.println(s+":"+v+"次,共"+l*v+"元");
        }


    }

    @Override
    public Winn judge(long periodsNum) {
        Winn num = winnDao.selectOne(new QueryWrapper<Winn>().eq("num", periodsNum));
        return num;
    }

    @Override
    public User selectUser(String userName) {
        return winnDao.selectUser(userName);
    }
}
